package com.study.ssm.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.study.ssm.po.ItemsCustom;
import com.study.ssm.service.ItemsService;

@Controller
//定义url的根路径，访问时路径：根路径＋方法url
@RequestMapping("/items")
public class ItemsController {
	
	//注入service
	@Autowired
	private ItemsService itemsService;
	
	@RequestMapping("/queryItems")
	public ModelAndView queryItems() throws Exception {
		
		List<ItemsCustom> itemsCustomsList = itemsService.findItemsList(null);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("itemsList", itemsCustomsList);
		// 指定逻辑视图名
		modelAndView.setViewName("itemsList");
		return modelAndView;
	}
	
	@RequestMapping(value="/editItem",method={RequestMethod.GET})
	public String editItem(Model model,Integer id) throws Exception {
		
		//调用 service查询商品信息
		ItemsCustom itemsCustom = itemsService.findItemsById(id);
		
		model.addAttribute("item", itemsCustom);
		
		return "editItem";
	}
	
	@RequestMapping(value="/editItemSubmit",method={RequestMethod.POST})
	public String editItemSubmit(Integer id,ItemsCustom itemsCustom)throws Exception{
		
		//调用service接口更新商品信息
		itemsService.updateItems(id, itemsCustom);
		//请求重定向
		return "redirect:queryItems.action";
		
	}
	
}
